package com.xingkeduo.pojo.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.net.URLEncoder;
import java.util.List;

/**
 * @Description:
 * @Author:gaozifeng
 * @Date 2019/4/19 14:10
 **/
public class JsonUtils {


    public static String getString(String json) {
        JSONObject jsonObject = JSON.parseObject(json);
        String status = jsonObject.getString("status");
        if (status.equals("0")) {
            return jsonObject.getString("msg");
        } else {
            //success
            return jsonObject.getString("data");
        }
    }

    /**
     * 加密返回参数
     *
     * @param key:
     * @param value:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/6 16:04
     */
    public static String encodeParam(String key, Object value) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(key, value);
        String encode = URLEncoder.encode(jsonObject.toJSONString());
        return encode;
    }

    public static String encodeList(List<Long> value) {
        JSONArray array = new JSONArray();
        for (Long va: value) {
            array.add(va);
        }
        String encode = URLEncoder.encode(array.toJSONString());
        return encode;
    }
}
